package com.tusuo.stds.ui.name;

import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.andreabaccega.widget.FormEditText;
import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.Utils;
import com.tusuo.stds.util.validator.MobilePhoneNumberValidator;
import com.tusuo.stds.util.validator.NameValidator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;
import rx.functions.Action0;

@EActivity(R.layout.activity_set_name)
public class SetNameActivity extends BaseToolbarActivity<SetNameView, SetNamePresenter>
                                implements SetNameView {

    @ViewById(R.id.set_name_et_name)
    FormEditText etName;

    @StringRes(R.string.toolbar_title_set_name)
    String toolbarTitle;
    @StringRes(R.string.err_msg_name)
    String strErrMsgName;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    @AfterViews
    void afterViews() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.MenuAction menuAction = new ToolbarOwner.MenuAction("完成", new Action0() {
            @Override
            public void call() {
                if (Utils.UI.isFastDoubleClick()) {
                    return;
                }

                if (etName.testValidity()) {
                    presenter.setName(etName.getText().toString());
                }

            }
        }, 0);

        menuActionList.add(menuAction);

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

        String name = getIntent().getStringExtra("name");
        if (name == null) {
            name = "";
        }

        etName.addValidator(new NameValidator(strErrMsgName));

        etName.setText(name);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public SetNamePresenter createPresenter() {
        return new SetNamePresenter(this);
    }

    @Override
    public void backToMainPage() {
        ApplicationHelper.getInstance().removeActivity(this);
    }

    //    @Click(R.id.set_name_btn_save)
//    void clickSaveBtn() {
//        if (Utils.UI.isFastDoubleClick()) {
//            return;
//        }
//
//        presenter.setName(etName.getText().toString());
//    }
}
